package net.towee.client.com.stuff.view;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

import net.towee.client.com.stuff.presenter.GroupShowerPresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.ListItemWidget;
import net.towee.client.widgets.UnorderedListWidget;

public class GroupShowerView extends FlowPanel implements Display {
	private class RightSlideAnimation extends Animation {
		private boolean showed = false;
		private double width;
		
		@Override
		protected void onStart() {
			super.onStart();
			width = getParent().getOffsetWidth();
			
			if(showed) {
				getElement().getStyle().setProperty("height", getElement().getOffsetHeight()-2+"px");
				getElement().getStyle().setProperty("position", "absolute");
				getElement().getStyle().setZIndex(0);
			}
		}
		
		@Override
		protected void onComplete() {
			super.onComplete();
			showed = !showed;
			getElement().getStyle().setProperty("width", "100%");
			
			if(!showed) 
				removeFromParent();
		}
		
		@Override
		protected void onUpdate(double progress) {
			progress = (showed)?1-progress:progress;
			
			// Change height and opacity
			if(!showed)
				getElement().getStyle().setProperty("width", width*progress+"px");
			
			if(showed)
				getElement().getStyle().setOpacity(1*progress);
		}
		
		public boolean isShowed() {
			return showed;
		}
	}
	
	private class HorizontalTabs extends UnorderedListWidget {
		private Widget activeTab;
		
		public HorizontalTabs() {
			getElement().setAttribute("tabs", "");
			
			addTab( constants.information(), info, true );
			addTab( constants.users(), users, false );
			addTab( constants.roles(), roles, false );
			addTab( constants.requests(), requests, false );
		}

		private void addTab(String name, final FlowPanel info, boolean active) {
			final ListItemWidget tab = new ListItemWidget( name );
			tab.getElement().setAttribute("tab", "");
			tab.addClickHandler( new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					container.clear();
					container.add( info );
					
					if(activeTab != null)
						activeTab.getElement().setAttribute("tab", "");
					
					activeTab = tab;
					activeTab.getElement().setAttribute("tab", "active");
				}
			});
			
			if(active) {
				container.clear();
				container.add( info );
				activeTab = tab;
				activeTab.getElement().setAttribute("tab", "active");
			}
			
			add(tab);
		}
	}
	
	private final ToweeConstants constants;
	private final ClientResource resource;
	private final RightSlideAnimation animation;
	private final FlowPanel container;
	
	private final FlowPanel info = new FlowPanel();
	private final FlowPanel users = new FlowPanel();
	private final FlowPanel roles = new FlowPanel();
	private final FlowPanel requests = new FlowPanel();
	
	@Inject
	public GroupShowerView(ToweeConstants constants, ClientResource resource) {
		this.constants = constants;
		this.resource = resource;
		this.animation = new RightSlideAnimation();
		this.container = new FlowPanel();
		
		// Set style
		container.getElement().setAttribute("container", "");
		setStyleName( resource.css().stuffGroupShow() );
		
		// Add tabs and container
		add(new HorizontalTabs());
		add(container);
	}

	@Override
	public void setGroupName(String name) {
		
	}

	@Override
	public HasWidgets getGroupInformationContainer() {
		return info;
	}

	@Override
	public HasWidgets getRolesContainer() {
		return roles;
	}

	@Override
	public HasWidgets getUsersContainer() {
		return users;
	}

	@Override
	public HasWidgets getRequestsContainer() {
		return requests;
	}

	@Override
	public void showInfo() {
		animation.run(400);		
	}

}
